package com.welink.cmp.meatdata.command;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.welink.cmp.dao.metadata.DataCenterDaoService;
import com.welink.cmp.dao.metadata.dataobject.DataCenterDO;
import com.welink.cmp.dto.metadata.datacenter.DataCenterCO;
import com.welink.cmp.dto.metadata.datacenter.DatacenterAddCmd;
import com.welink.cmp.dto.metadata.datacenter.DatacenterPageQry;
import com.welink.cmp.dto.metadata.datacenter.DatacenterUpdCmd;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DataCenterCmdExe {

    @Autowired
    private DataCenterDaoService dataCenterDaoService;

    public Long save(DatacenterAddCmd addCmd) {
        DataCenterDO dataCenter = new DataCenterDO();
        BeanUtils.copyProperties(addCmd, dataCenter);
        if (addCmd.isNeedsOperator()){
            dataCenter.setCreateBy(addCmd.getOperator());
        }
        dataCenterDaoService.save(dataCenter);
        return dataCenter.getId();
    }

    public Boolean update(DatacenterUpdCmd updCmd) {
        DataCenterDO dataCenter = new DataCenterDO();
        BeanUtils.copyProperties(updCmd, dataCenter);
        if (updCmd.isNeedsOperator()){
            dataCenter.setUpdateBy(updCmd.getOperator());
        }
        return dataCenterDaoService.updateById(dataCenter);
    }

    public DataCenterCO queryById(Long id) {
        DataCenterDO dataCenter = dataCenterDaoService.getById(id);
        DataCenterCO dataCenterCO = new DataCenterCO();
        BeanUtils.copyProperties(dataCenter, dataCenterCO);
        return dataCenterCO;
    }

    public Page<DataCenterDO> pageQuery(DatacenterPageQry pageQry) {
        return dataCenterDaoService.pageQry(pageQry);
    }
}